float4x4 World;
float4x4 View;
float4x4 Projection;

float4 LightColor;
float3 LightPosition;

float4 SpecularColor;

float4 AmbientColor;

float SpecularPower;
float SpecularIntensity;

float3 CameraPosition;


struct VertexShaderInput
{
    float4 Position : POSITION0;
	float3 Normal : NORMAL0;
	float4 Color : COLOR0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
	float4 Color : COLOR0;
};


// Per Vertex lighting with phong model
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
	
	float3 worldNormal =  mul(input.Normal, World);
    worldPosition = worldPosition / worldPosition.w;

    float3 directionToLight = normalize(LightPosition - worldPosition.xyz);
    float diffuseIntensity = saturate( dot(directionToLight, worldNormal));
    float4 diffuse = LightColor * diffuseIntensity;
    
    // find reflection
    float3 reflectionVector = normalize(reflect(-directionToLight, worldNormal));
    
    // approximate the specular color based on how close it is to the direction to the camera
    float3 directionToCamera = normalize(CameraPosition - worldPosition.xyz);
    float4 specular = SpecularColor * SpecularIntensity *
						pow(saturate(dot(reflectionVector, directionToCamera)),
                        SpecularPower);
	
    output.Color = specular + diffuse + AmbientColor;
    output.Color.a = 1.0;
	
    return output;
}


float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
	return input.Color;
}



technique Technique1
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

